/*
 *  (C) Copyright 2008 Insula T.I. Ltda.
 * 
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.orm.dao.query.spec;

/**
 * @author rebola
 * @author yanaga
 * @since 1.0
 * @see Spec
 */
public class LikeSpec extends AbstractSingleValuePropertySpec {
	private static final long serialVersionUID = 1L;

	private boolean caseSensitive;

	/**
	 * This constructor will create a <strong>case-sensitive</strong> like
	 * comparison.
	 */
	public LikeSpec(final String propertyName, final String value) {
		this(propertyName, value, true);
	}

	/**
	 * This constructor will create a like comparison with case-sensitive
	 * awareness depending on the <code>caseSensitive</code>.
	 * @param caseSensitive If <code>false</code>, values are converted to
	 * lowercase before comparison.
	 */
	public LikeSpec(final String propertyName, final String value, final boolean caseSensitive) {
		super(propertyName, value);
		setCaseSensitive(caseSensitive);
	}

	public boolean isCaseSensitive() {
		return caseSensitive;
	}

	private void setCaseSensitive(final boolean caseSensitive) {
		this.caseSensitive = caseSensitive;
		setValue(caseSensitive ? getValue().toString() : getValue().toString().toLowerCase());
	}

	@Override
	public <V> V accept(SpecVisitor<V> visitor) {
		return visitor.visit(this);
	}

}
